<?php
if ($level != 3) {
	echo "Bạn không có quyền vào trang này.";
	exit();
}
echo "<table width=90% align=center cellpadding=2 cellspacing=0 class=border><form method=post>";
echo "<tr><td align=center class=title width=5%>STT</td><td class=title style='border-right:0'>Tên thể loại</td></tr>";
	$cat_query = $mysql->query("SELECT * FROM ".$tb_prefix."cat WHERE (sub_id IS NULL OR sub_id = 0) ORDER BY cat_order ASC");
		while ($cat = $mysql->fetch_array($cat_query)) {
			echo "<tr align=center><td colspan=2 class=cat_title>".$cat['cat_title']."</td></tr>";
			$iz = $cat['cat_order'];
			echo "<tr><td align=center class=fr><input onclick=this.select() type=text name='o".$cat['cat_id']."' value=$iz size=2 style='text-align:center'></td><td class=fr_2><a href='?act=cat&del_id=".$cat['cat_id']."'>Xóa</a> - <a href='?act=cat&mode=edit&id=".$cat['cat_id']."'><b>".$cat['cat_name']."</b></a></td></tr>";
			$sub_query = $mysql->query("SELECT * FROM ".$tb_prefix."cat WHERE sub_id = '".$cat['cat_id']."' ORDER BY cat_order ASC");
			if ($mysql->num_rows($sub_query)) echo "<tr><td class=fr_2>&nbsp;</td><td class=fr><table width=100% cellpadding=2 cellspacing=0 class=border>";
			while ($sub = $mysql->fetch_array($sub_query)) {
				$s_o = $sub['cat_order'];
				echo "<tr><td align=center class=fr width=5%><input onclick=this.select() type=text name='o".$sub['cat_id']."' value=$s_o size=2 style='text-align:center'></td><td class=fr_2><a href='?act=cat&del_id=".$sub['cat_id']."'>Xóa</a> - <a href='?act=cat&mode=edit&id=".$sub['cat_id']."'><b>".$sub['cat_name']."</b></a></td></tr>";
			}
			if ($mysql->num_rows($sub_query)) echo "</table></td></tr>";
		}
echo '<tr><td colspan="2" align="center"><input type="submit" name="sbm" class=submit value="Sửa thứ tự"></td></tr>';
echo '</form></table>';
if ($_POST['sbm']) {
	$z = array_keys($_POST);
	$q = $mysql->query("SELECT cat_id FROM ".$tb_prefix."cat");
	for ($i=0;$i<$mysql->num_rows($q);$i++) {
		$id = split('o',$z[$i]);
		$od = ${$z[$i]};
		$mysql->query("UPDATE ".$tb_prefix."cat SET cat_order = '$od' WHERE cat_id = '".$id[1]."'");
		echo "<script language='JavaScript'>{ window.parent.location='?act=list_cat' }</script>";
	}
}
?>